package com.jd.nb.controller;

import com.jd.nb.common.Data;
import com.jd.nb.common.Query;
import com.jd.nb.common.RS;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

@Controller
public class TestController {

    @RequestMapping(value = "/test", method = RequestMethod.GET)
    public String test(ModelMap modelMap){
        System.out.println("has done");
        ModelAndView mv = new ModelAndView("test");
        mv.addObject("tst","needSuccess");

        modelMap.put("foot", "need Success");
        return "test";
    }

    @RequestMapping(value = "/vee", method = RequestMethod.GET)
    public ModelAndView kk(ModelMap modelMap, HttpServletRequest request, HttpServletResponse response){
        System.out.println("vee worked");
        modelMap.put("foot", "method of vee worked");
        modelMap.put("hand", "val of vee");
        List list = new ArrayList();
        list.add("he");
        list.add("ll");
        list.add("oW");
        list.add("or");
        list.add("ld");
        modelMap.put("lists", list);

        Cookie cookie = new Cookie("name", "test");
        cookie.setPath(request.getContextPath());
        response.addCookie(cookie);

        ModelAndView mv = new ModelAndView("duplicate");
        return mv;
    }

    @RequestMapping(value = "/testModelAndView")
    public String diff(Model model){
        model.addAttribute("info", "message from model");
        return "test";
    }

    @RequestMapping(value = "/testSpace")
    public String kk(Model model){
        model.addAttribute("rewardMeet", 100);
        model.addAttribute("flag", 0);
        model.addAttribute("num", 122);
        return "test";
    }

    /**
     * 此测试用例中，RS中并没有success字段，只有isSuccess方法，即便如此，返回结果中仍会出现success字段
     * 将RS转换成json数据时，会扫描RS中所有的set、get、is方法，并将其后的名称设置成一个字段。
     * RS中有isSuccess方法，扫描时也就会将success作为一个字段显示，该字段结果为isSuceess方法的结果。
     *
     * 测试过程中出现过406错误，引入jsckson依赖后正常
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/testBean")
    public RS testBean(){
        RS rs = new RS();
        rs.setCode(0);
        rs.setMsg("test");
        return rs;
    }

    @ResponseBody
    @RequestMapping(value = "/testData")
    public RS testData(){
        Data data= new Data();
        RS rs = new RS();
//        rs.setData(data);

        return rs;
    }

    @ResponseBody
    @RequestMapping(value = "/testData2")
    public RS testData2(){
        Data data= null;
        RS rs = new RS();
        rs.setData(data);

        return rs;
    }

    @ResponseBody
    @RequestMapping(value = "/testException")
    public RS testExeption(){
        RS rs = new RS();
        try {
            int i = 1/0;
            System.out.println("test whether occured");
        }catch (Exception e){
            System.out.println("i catched the exception");
            rs.setMsg("exception ocured");
        }
        return rs;
    }


    @ResponseBody
    @RequestMapping(value = "/kkkk")
    public void test(String key, Integer in, Long lo){
        System.out.println(key+ in+ lo);
    }

    /**
     * 包装类query中，id为包装类Integer，code为int，在传值过程中，
     * 为包装类的id即便不传值也不会出问题，但code在不传值的情况下则报400的错误
     *
     * 但需要注意的是因为包装类类型的id允许不传值，在不传值的情况下其值为null，
     * 如果直接对id进行比较的话将会报异常，所以这种情况下需要加上是否为null的判断
     * @param query
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/query")
    public RS testQuery(Query query){
        RS rs = new RS();
        rs.setMsg(query.toString());
        return rs;
    }


}
